`
https://leetcode.cn/problems/nearest-exit-from-entrance-in-maze/
`

/**
 * @param {character[][]} maze
 * @param {number[]} entrance
 * @return {number}
 */
var nearestExit = function (maze, entrance) {
  const m = maze.length, n = maze[0].length
  const dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]

  const q = [entrance]
  const visited = Array.from({ length: m }, () => new Array(n).fill(false))
  visited[entrance[0]][entrance[1]] = true
  let dist = 0

  // 从 entrance 开始像四周扩散
  while (q.length) {
    const sz = q.length
    dist++
    // 扩散当前队列中的所有节点
    for (let i = 0; i < sz; i++) {
      const cur = q.shift()
      // 每个节点都会尝试向上下左右四个方向扩展一步
      for (const dir of dirs) {
        const x = cur[0] + dir[0]
        const y = cur[1] + dir[1]
        if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y] || maze[x][y] === '+') {
          continue;
        }
        if (x === 0 || x === m - 1 || y === 0 || y === n - 1) {
          // 走到边界（出口）
          return dist
        }
        visited[x][y] = true
        q.push([x, y])
      }
    }
  }

  return -1
};